package com.dmp.storm.bolt.extend;

import com.dmp.common.mybatis.ExtendService;
import com.dmp.storm.bolt.BaseMapBolt;
import com.dmp.storm.topology.XmlTopologyCtx;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.File;
import java.util.Map;

/**
 * 扩充字段信息
 * 
 * @author june
 *
 */

public class ExtendBolt extends BaseMapBolt {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2718627510354718799L;

	private static Logger logger = LoggerFactory.getLogger(ExtendBolt.class);

	private ApplicationContext appContext;
	private ExtendService service;
	// 配置参数
	// private String extServiceName = "billFieldExtService";
	private String extServiceName = "fieldService";
	private String extServiceConfig = "spring-context-extend.xml";

	public ExtendService getService() {
		return service;
	}

	public void setService(ExtendService service) {
		this.service = service;
	}

	public String getExtServiceName() {
		return extServiceName;
	}

	public String getExtServiceConfig() {
		return extServiceConfig;
	}

	@Override
	public void prepare() {
		 File file = new File("../etc/" + extServiceConfig);
	        if (file.exists()) {
	        	System.out.println("bv2");
	        	System.out.println(file.getAbsolutePath());
	        	appContext = new ClassPathXmlApplicationContext(file.getAbsolutePath());
	        } else {
	        	System.out.println("bv1");
	        	appContext = new ClassPathXmlApplicationContext(extServiceConfig);
	        }
		
		//appContext = new ClassPathXmlApplicationContext(extServiceConfig);
		service = appContext.getBean(extServiceName, ExtendService.class);
		
		System.out.println("ExtendBolt1");

		logger.info("extend service:{}", service);
	}

	/**
	 * 主执行方法
	 */
	@Override
	public Map<String, Object> process(Map<String, Object> map) {
		Map<String, Object> record = map;

		if (service != null) {
			// System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + record);
			if (record != null && record.size() > 0) {
				service.extend(record);
			}
		}

		// System.out.println("@@@@@@@@@@@@@@@@@@@@@@@" + record);

		return record;
	}

	public void setExtServiceConfig(String extServiceConfig) {
		this.extServiceConfig = extServiceConfig;
	}

	public void setExtServiceName(String extServiceName) {
		this.extServiceName = extServiceName;
	}

}
